09fe06e601d5916d42d06e7ad15895f4b73763a9
[lhc/web/wiklou.git] / resources / jquery / jquery.makeCollapsible.js
1 /**
2 * jQuery makeCollapsible
3 *
4 * This will enable collapsible-functionality on all passed elements.
5 * - Will prevent binding twice to the same element.
6 * - Initial state is expanded by default, this can be overriden by adding class
7 * "mw-collapsed" to the "mw-collapsible" element.
8 * - Elements made collapsible have jQuery data "mw-made-collapsible" set to true.
9 * - The inner content is wrapped in a "div.mw-collapsible-content" (except for tables and lists).
10 *
11 * @author Krinkle, 2011-2012
12 *
13 * Dual license:
14 * @license CC-BY 3.0 <http://creativecommons.org/licenses/by/3.0>
15 * @license GPL2 <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>
16 */
17 ( function ( $, mw ) {
18 var lpx = 'jquery.makeCollapsible> ';
19
20 /**
21 * @param {jQuery} $collapsible
22 * @param {string} action The action this function will take ('expand' or 'collapse').
23 * @param {jQuery|null} [optional] $defaultToggle
24 * @param {Object|undefined} options
25 */
26 function toggleElement( $collapsible, action, $defaultToggle, options ) {
27 var $collapsibleContent, $containers, hookCallback;
28 options = options || {};
29
30 // Validate parameters
31
32 // $collapsible must be an instance of jQuery
33 if ( !$collapsible.jquery ) {
34 return;
35 }
36 if ( action !== 'expand' && action !== 'collapse' ) {
37 // action must be string with 'expand' or 'collapse'
38 return;
39 }
40 if ( $defaultToggle === undefined ) {
41 $defaultToggle = null;
42 }
43 if ( $defaultToggle !== null && !$defaultToggle.jquery ) {
44 // is optional (may be undefined), but if defined it must be an instance of jQuery.
45 // If it's not, abort right away.
46 // After this $defaultToggle is either null or a valid jQuery instance.
47 return;
48 }
49
50 // Trigger a custom event to allow callers to hook to the collapsing/expanding,
51 // allowing the module to be testable, and making it possible to
52 // e.g. implement persistence via cookies
53 $collapsible.trigger( action === 'expand' ? 'beforeExpand.mw-collapsible' : 'beforeCollapse.mw-collapsible' );
54 hookCallback = function () {
55 $collapsible.trigger( action === 'expand' ? 'afterExpand.mw-collapsible' : 'afterCollapse.mw-collapsible' );
56 };
57
58 // Handle different kinds of elements
59
60 if ( !options.plainMode && $collapsible.is( 'table' ) ) {
61 // Tables
62 $containers = $collapsible.find( '> tbody > tr' );
63 if ( $defaultToggle ) {
64 // Exclude table row containing togglelink
65 $containers = $containers.not( $defaultToggle.closest( 'tr' ) );
66 }
67
68 if ( action === 'collapse' ) {
69 // Hide all table rows of this table
70 // Slide doesn't work with tables, but fade does as of jQuery 1.1.3
71 // http://stackoverflow.com/questions/467336#920480
72 if ( options.instantHide ) {
73 $containers.hide();
74 hookCallback();
75 } else {
76 $containers.stop( true, true ).fadeOut( hookCallback );
77 }
78 } else {
79 $containers.stop( true, true ).fadeIn( hookCallback );
80 }
81
82 } else if ( !options.plainMode && ( $collapsible.is( 'ul' ) || $collapsible.is( 'ol' ) ) ) {
83 // Lists
84 $containers = $collapsible.find( '> li' );
85 if ( $defaultToggle ) {
86 // Exclude list-item containing togglelink
87 $containers = $containers.not( $defaultToggle.parent() );
88 }
89
90 if ( action === 'collapse' ) {
91 if ( options.instantHide ) {
92 $containers.hide();
93 hookCallback();
94 } else {
95 $containers.stop( true, true ).slideUp( hookCallback );
96 }
97 } else {
98 $containers.stop( true, true ).slideDown( hookCallback );
99 }
100
101 } else {
102 // Everything else: <div>, <p> etc.
103 $collapsibleContent = $collapsible.find( '> .mw-collapsible-content' );
104
105 // If a collapsible-content is defined, act on it
106 if ( !options.plainMode && $collapsibleContent.length ) {
107 if ( action === 'collapse' ) {
108 if ( options.instantHide ) {
109 $collapsibleContent.hide();
110 hookCallback();
111 } else {
112 $collapsibleContent.slideUp( hookCallback );
113 }
114 } else {
115 $collapsibleContent.slideDown( hookCallback );
116 }
117
118 // Otherwise assume this is a customcollapse with a remote toggle
119 // .. and there is no collapsible-content because the entire element should be toggled
120 } else {
121 if ( action === 'collapse' ) {
122 if ( options.instantHide ) {
123 $collapsible.hide();
124 hookCallback();
125 } else {
126 if ( $collapsible.is( 'tr' ) || $collapsible.is( 'td' ) || $collapsible.is( 'th' ) ) {
127 $collapsible.fadeOut( hookCallback );
128 } else {
129 $collapsible.slideUp( hookCallback );
130 }
131 }
132 } else {
133 if ( $collapsible.is( 'tr' ) || $collapsible.is( 'td' ) || $collapsible.is( 'th' ) ) {
134 $collapsible.fadeIn( hookCallback );
135 } else {
136 $collapsible.slideDown( hookCallback );
137 }
138 }
139 }
140 }
141 }
142
143 /**
144 * Handles clicking on the collapsible element toggle and other
145 * situations where a collapsible element is toggled (e.g. the initial
146 * toggle for collapsed ones).
147 *
148 * @param {jQuery} $toggle the clickable toggle itself
149 * @param {jQuery} $collapsible the collapsible element
150 * @param {jQuery.Event|null} e either the event or null if unavailable
151 * @param {Object|undefined} options
152 */
153 function togglingHandler( $toggle, $collapsible, e, options ) {
154 var wasCollapsed, $textContainer, collapseText, expandText;
155
156 if ( options === undefined ) {
157 options = {};
158 }
159
160 if ( e ) {
161 // Don't fire if a link was clicked, if requested (for premade togglers by default)
162 if ( options.linksPassthru && $.nodeName( e.target, 'a' ) ) {
163 return;
164 } else {
165 e.preventDefault();
166 e.stopPropagation();
167 }
168 }
169
170 wasCollapsed = $collapsible.hasClass( 'mw-collapsed' );
171
172 // Toggle the state of the collapsible element (that is, expand or collapse)
173 $collapsible.toggleClass( 'mw-collapsed', !wasCollapsed );
174
175 // Toggle the mw-collapsible-toggle classes, if requested (for default and premade togglers by default)
176 if ( options.toggleClasses ) {
177 $toggle
178 .toggleClass( 'mw-collapsible-toggle-collapsed', !wasCollapsed )
179 .toggleClass( 'mw-collapsible-toggle-expanded', wasCollapsed );
180 }
181
182 // Toggle the text ("Show"/"Hide"), if requested (for default togglers by default)
183 if ( options.toggleText ) {
184 collapseText = options.toggleText.collapseText;
185 expandText = options.toggleText.expandText;
186
187 $textContainer = $toggle.find( '> a' );
188 if ( !$textContainer.length ) {
189 $textContainer = $toggle;
190 }
191 $textContainer.text( wasCollapsed ? collapseText : expandText );
192 }
193
194 // And finally toggle the element state itself
195 toggleElement( $collapsible, wasCollapsed ? 'expand' : 'collapse', $toggle, options );
196 }
197
198 /**
199 * Toggles collapsible and togglelink class and updates text label.
200 *
201 * @param {jQuery} $that
202 * @param {jQuery.Event} e
203 * @param {Object|undefined} options
204 */
205 function toggleLinkDefault( $that, e, options ) {
206 var $collapsible = $that.closest( '.mw-collapsible' );
207 options = $.extend( { toggleClasses: true }, options );
208 togglingHandler( $that, $collapsible, e, options );
209 }
210
211 /**
212 * Toggles collapsible and togglelink class.
213 *
214 * @param {jQuery} $that
215 * @param {jQuery.Event} e
216 * @param {Object|undefined} options
217 */
218 function toggleLinkPremade( $that, e, options ) {
219 var $collapsible = $that.eq( 0 ).closest( '.mw-collapsible' );
220 options = $.extend( { toggleClasses: true }, options );
221 togglingHandler( $that, $collapsible, e, options );
222 }
223
224 /**
225 * Toggles customcollapsible.
226 *
227 * @param {jQuery} $that
228 * @param {jQuery.Event} e
229 * @param {Object|undefined} options
230 * @param {jQuery} $collapsible
231 */
232 function toggleLinkCustom( $that, e, options, $collapsible ) {
233 options = $.extend( { linksPassthru: true }, options );
234 togglingHandler( $that, $collapsible, e, options );
235 }
236
237 /**
238 * Make any element collapsible.
239 *
240 * Supported options:
241 * - collapseText: text to be used for the toggler when clicking it would
242 * collapse the element. Default: the 'data-collapsetext' attribute of
243 * the collapsible element or the content of 'collapsible-collapse'
244 * message.
245 * - expandText: text to be used for the toggler when clicking it would
246 * expand the element. Default: the 'data-expandtext' attribute of
247 * the collapsible element or the content of 'collapsible-expand'
248 * message.
249 * - collapsed: boolean, whether to collapse immediately. By default
250 * collapse only if the elements has the 'mw-collapsible' class.
251 * - $customTogglers: jQuerified list of elements to be used as togglers
252 * for this collapsible element. By default, if the collapsible element
253 * has an id attribute like 'mw-customcollapsible-XXX', elements with a
254 * *class* of 'mw-customtoggle-XXX' are made togglers for it.
255 * - plainMode: boolean, whether to use a "plain mode" when making the
256 * element collapsible - that is, hide entire tables and lists (instead
257 * of hiding only all rows but first of tables, and hiding each list
258 * item separately for lists) and don't wrap other elements in
259 * div.mw-collapsible-content. May only be used with custom togglers.
260 */
261 $.fn.makeCollapsible = function ( options ) {
262 return this.each(function () {
263 var $collapsible, collapsetext, expandtext, $toggle, $toggleLink, $firstItem, collapsibleId,
264 $customTogglers, firstval;
265
266 if ( options === undefined ) {
267 options = {};
268 }
269
270 // Ensure class "mw-collapsible" is present in case .makeCollapsible()
271 // is called on element(s) that don't have it yet.
272 $collapsible = $(this).addClass( 'mw-collapsible' );
273
274 // Return if it has been enabled already.
275 if ( $collapsible.data( 'mw-made-collapsible' ) ) {
276 return;
277 } else {
278 $collapsible.data( 'mw-made-collapsible', true );
279 }
280
281 // Use custom text or default?
282 collapsetext = options.collapseText || $collapsible.attr( 'data-collapsetext' ) || mw.msg( 'collapsible-collapse' );
283 expandtext = options.expandText || $collapsible.attr( 'data-expandtext' ) || mw.msg( 'collapsible-expand' );
284
285 // Create toggle link with a space around the brackets (&nbsp;[text]&nbsp;)
286 $toggleLink =
287 $( '<a href="#"></a>' )
288 .text( collapsetext )
289 .wrap( '<span class="mw-collapsible-toggle"></span>' )
290 .parent()
291 .prepend( '&nbsp;[' )
292 .append( ']&nbsp;' )
293 .on( 'click.mw-collapsible', function ( e, opts ) {
294 opts = $.extend( { toggleText: { collapseText: collapsetext, expandText: expandtext } }, options, opts );
295 toggleLinkDefault( $(this), e, opts );
296 } );
297
298 // Check if this element has a custom position for the toggle link
299 // (ie. outside the container or deeper inside the tree)
300 if ( options.$customTogglers ) {
301 $customTogglers = $( options.$customTogglers );
302 } else {
303 collapsibleId = $collapsible.attr( 'id' ) || '';
304 if ( collapsibleId.indexOf( 'mw-customcollapsible-' ) === 0 ) {
305 mw.log( lpx + 'Found custom collapsible: #' + collapsibleId );
306 $customTogglers = $( '.' + collapsibleId.replace( 'mw-customcollapsible', 'mw-customtoggle' ) );
307
308 // Double check that there is actually a customtoggle link
309 if ( !$customTogglers.length ) {
310 mw.log( lpx + '#' + collapsibleId + ': Missing toggler!' );
311 }
312 }
313 }
314
315 // Bind the custom togglers
316 if ( $customTogglers && $customTogglers.length ) {
317 $customTogglers.on( 'click.mw-collapsible', function ( e, opts ) {
318 opts = $.extend( {}, options, opts );
319 toggleLinkCustom( $(this), e, opts, $collapsible );
320 } );
321
322 // Initial state
323 if ( options.collapsed || $collapsible.hasClass( 'mw-collapsed' ) ) {
324 // Remove here so that the toggler goes in the right direction,
325 // It re-adds the class.
326 $collapsible.removeClass( 'mw-collapsed' );
327 toggleLinkCustom( $customTogglers, null, $.extend( { instantHide: true }, options ), $collapsible );
328 }
329
330 // If this is not a custom case, do the default:
331 // Wrap the contents and add the toggle link
332 } else {
333 // Elements are treated differently
334 if ( $collapsible.is( 'table' ) ) {
335 // The toggle-link will be in one the the cells (td or th) of the first row
336 $firstItem = $collapsible.find( 'tr:first th, tr:first td' );
337 $toggle = $firstItem.find( '> .mw-collapsible-toggle' );
338
339 // If theres no toggle link, add it to the last cell
340 if ( !$toggle.length ) {
341 $firstItem.eq(-1).prepend( $toggleLink );
342 } else {
343 $toggleLink = $toggle.off( 'click.mw-collapsible' ).on( 'click.mw-collapsible', function ( e, opts ) {
344 opts = $.extend( {}, options, opts );
345 toggleLinkPremade( $toggle, e, opts );
346 } );
347 }
348
349 } else if ( $collapsible.is( 'ul' ) || $collapsible.is( 'ol' ) ) {
350 // The toggle-link will be in the first list-item
351 $firstItem = $collapsible.find( 'li:first' );
352 $toggle = $firstItem.find( '> .mw-collapsible-toggle' );
353
354 // If theres no toggle link, add it
355 if ( !$toggle.length ) {
356 // Make sure the numeral order doesn't get messed up, force the first (soon to be second) item
357 // to be "1". Except if the value-attribute is already used.
358 // If no value was set WebKit returns "", Mozilla returns '-1', others return null or undefined.
359 firstval = $firstItem.attr( 'value' );
360 if ( firstval === undefined || !firstval || firstval === '-1' || firstval === -1 ) {
361 $firstItem.attr( 'value', '1' );
362 }
363 $collapsible.prepend( $toggleLink.wrap( '<li class="mw-collapsible-toggle-li"></li>' ).parent() );
364 } else {
365 $toggleLink = $toggle.off( 'click.mw-collapsible' ).on( 'click.mw-collapsible', function ( e, opts ) {
366 opts = $.extend( {}, options, opts );
367 toggleLinkPremade( $toggle, e, opts );
368 } );
369 }
370
371 } else { // <div>, <p> etc.
372
373 // The toggle-link will be the first child of the element
374 $toggle = $collapsible.find( '> .mw-collapsible-toggle' );
375
376 // If a direct child .content-wrapper does not exists, create it
377 if ( !$collapsible.find( '> .mw-collapsible-content' ).length ) {
378 $collapsible.wrapInner( '<div class="mw-collapsible-content"></div>' );
379 }
380
381 // If theres no toggle link, add it
382 if ( !$toggle.length ) {
383 $collapsible.prepend( $toggleLink );
384 } else {
385 $toggleLink = $toggle.off( 'click.mw-collapsible' ).on( 'click.mw-collapsible', function ( e, opts ) {
386 opts = $.extend( {}, options, opts );
387 toggleLinkPremade( $toggle, e, opts );
388 } );
389 }
390 }
391 }
392
393 // Initial state (only for those that are not custom,
394 // because the initial state of those has been taken care of already).
395 if (
396 ( options.collapsed || $collapsible.hasClass( 'mw-collapsed' ) ) &&
397 ( !$customTogglers || !$customTogglers.length )
398 ) {
399 $collapsible.removeClass( 'mw-collapsed' );
400 // The collapsible element could have multiple togglers
401 // To toggle the initial state only click one of them (ie. the first one, eq(0) )
402 // Else it would go like: hide,show,hide,show for each toggle link.
403 // This is just like it would be in reality (only one toggle is clicked at a time).
404 $toggleLink.eq( 0 ).trigger( 'click', [ { instantHide: true } ] );
405 }
406 } );
407 };
408 }( jQuery, mediaWiki ) );